package Dao.Impl;

import Dao.OrdersDao;
import Domain.*;
import JDBCUtils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());


    @Override
    public void insertOrder(List<Product> p, User u, List<Cart> c, int payid) {
        System.out.println("dao");
        int Orderid= (int) (Math.random()*899999999+100000000);
        double odersum=0;
        Date now=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("YYY-MM-dd HH:mm:ss");//格式化时间显示
        String ordertime=sdf.format(now);
        String sql="insert into orders(id,tel,address,ordertime,ordersum,orderstatus,payid,userid) values(?,?,?,?,?,?,?,?)";
        for (Cart cc:c) {
            for (Product pp:p){
                if (cc.getProductid()==pp.getId()){
                    odersum=odersum+pp.getPrice()*cc.getCnum();
                }
            }
        }
        System.out.println("总价"+odersum);
        int i=template.update(sql,Orderid,u.getTel(),u.getAddress(),ordertime,odersum,"出货中",payid,u.getId());
        for (Product pp:p){
     String sql1="insert into t_orders_product_relation(ordersid,productid,price,p_numb) values(?,?,?,?)";
     String sql2="delete from t_cart_product_relation where id=?";
     String sql3="update product set num=? where id=?";
            for (Cart cc:c){
                if(cc.getProductid()==pp.getId())
       template.update(sql1,Orderid,pp.getId(),pp.getPrice(),cc.getCnum());
       template.update(sql2,cc.getId());
       template.update(sql3,pp.getNum()-cc.getCnum(),pp.getId());
            }
        }
    }

    @Override
    public void buyone(Product p, User u, int num, int payid) {
        int Orderid= (int) (Math.random()*899999999+100000000);
        double odersum=0;
        Date now=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("YYY-MM-dd HH:mm:ss");//格式化时间显示
        String ordertime=sdf.format(now);
        String sql="insert into orders(id,tel,address,ordertime,ordersum,orderstatus,payid,userid) values(?,?,?,?,?,?,?,?)";
       odersum=p.getPrice()*num;
        System.out.println("总价"+odersum);
            String sql1="insert into t_orders_product_relation(ordersid,productid,price,p_numb) values(?,?,?,?)";
            String sql3="update product set num=? where id=?";
            template.update(sql,Orderid,u.getTel(),u.getAddress(),ordertime,odersum,"出货中",payid,u.getId());
            template.update(sql1,Orderid,p.getId(),p.getPrice(),num);
            template.update(sql3,p.getNum()-num,p.getId());

        }

    @Override
    public List<Orders> selectorderbyuserid(int userid) {
        String sql="select * from orders where userid=?";
        return template.query(sql,new BeanPropertyRowMapper<>(Orders.class),userid);
    }

    @Override
    public List<topr2> oandpselect() {
        String sql= "select\n" +
                "d.id ,d.ordersid,d.productid,d.price,d.p_numb,p.name,p.picture\n" +
                "from\n" +
                "t_orders_product_relation d\n" +
                "join\n" +
                "product p\n" +
                "on\n" +
                "  d.productid=p.id\n" +
                "where\n" +
                "  1=1";
        return template.query(sql,new BeanPropertyRowMapper<>(topr2.class));
    }

    @Override
    public List<Orderselect> Orderselectbyid(int id) {
        String sql="select\n" +
                "    o.id , e.username,o.address , o.tel ,o.ordertime,o.orderstatus,p.name,pa.payment,o.ordersum \n" +
                "from\n" +
                "  orders o \n" +
                "join\n" +
                "  t_orders_product_relation d\n" +
                "on\n" +
                "  o.id=d.ordersid\n" +
                "join\n" +
                "  user_login e\n" +
                "on\n" +
                "  e.id=o.userid\n" +
                "join\n" +
                "   product p\n" +
                "on\n" +
                "  p.id=d.productid\n" +
                "join\n" +
                "   pays pa\n" +
                "on\n" +
                "  pa.id=o.payid\n" +

                "where\n" +
                "o.id=?";
        return template.query(sql,new BeanPropertyRowMapper<>(Orderselect.class),id);
    }

}


